﻿namespace IntellectShop.Web2.Hardware.Controllers
{
    using IntellectShop.Common;
    using IntellectShop.Web2.Hardware.ViewModels;
    using Ionic.Zip;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using System.Xml;

    [IntellectShop.Web2.Hardware.Code.NoCache]
    [IntellectShop.Web2.Hardware.Code.HandleErrorWithELMAH]
    public class FileController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = Globals.AdministratorRoleName)]
        public ActionResult UploadPriceList()
        {
            var result = new List<UploadFileResultViewModel>();

            foreach (string file in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf == null || hpf.ContentLength == 0)
                {
                    continue;
                }

                string savedFileName = this.GetPriceFileName();

                try
                {
                    if (Path.GetExtension(savedFileName) == ".zip")
                    {
                        using (ZipFile zip = new ZipFile(Encoding.UTF8))
                        {
                            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

                            zip.AddEntry(Path.GetFileName(hpf.FileName), hpf.InputStream);
                            zip.Save(savedFileName);
                        }
                    }
                    else
                    {
                        hpf.SaveAs(savedFileName);
                    }

                    result.Add(new UploadFileResultViewModel()
                    {
                        RecievedName = hpf.FileName,
                        SavedName = savedFileName,
                        RecievedLength = hpf.ContentLength
                    });
                }
                catch (Exception e)
                {
                    result.Add(new UploadFileResultViewModel()
                    {
                        RecievedName = hpf.FileName,
                        SavedName = savedFileName,
                        RecievedLength = hpf.ContentLength,
                        ErrorMessage = e.Message
                    });
                }
            }

            return View(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = Globals.AdministratorRoleName)]
        public ActionResult UploadCommodity(bool setNotActive)
        {
            var result = new List<ImportFileResultViewModel>();

            foreach (string file in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf == null || hpf.ContentLength == 0)
                {
                    continue;
                }

                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(hpf.InputStream);

                    IntellectShop.Common.Course.ImportFromXml(doc, HttpContext.User.Identity.Name);
                    IntellectShop.Common.Software.ImportFromXml(doc, HttpContext.User.Identity.Name);
                    IntellectShop.Common.Hardware.ImportFromXml(doc, setNotActive, HttpContext.User.Identity.Name);

                    result.Add(new ImportFileResultViewModel()
                    {
                        RecievedName = hpf.FileName,
                        RecievedLength = hpf.ContentLength
                    });
                }
                catch (Exception e)
                {
                    result.Add(new ImportFileResultViewModel()
                    {
                        RecievedName = hpf.FileName,
                        RecievedLength = hpf.ContentLength,
                        ErrorMessage = e.Message
                    });
                }
            }

            return View(result);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult DownloadPriceList()
        {
            string path = this.GetPriceFileName();
            string filename = Path.GetFileName(path);

            return File(path, "application/zip", filename);
        }

        private string GetPriceFileName()
        {
            return Server.MapPath(ConfigurationManager.AppSettings["UploadedPriceList"]);
        }
    }
}
